package com.beiding.editanalyzeframe;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author 丁常磊
 * @date 2022/6/24 13:58
 */

public class DocBuilder {

    /**
     * 所有的行
     */
    private List<String> lines = new ArrayList<>();

    public int add(String line) {
        this.lines.add(line);
        return this.lines.size();
    }

    public int remove(int lineNumber) {
        this.lines.remove(lineNumber);
        return this.lines.size();
    }

    public String get(int lineNumber) {
        if (lines.size() > lineNumber) {
            return lines.get(lineNumber);
        }
        return null;
    }

    public int size() {
        return lines.size();
    }

    public String set(int lineNumber, String line) {
        return lines.set(lineNumber, line);
    }

    /**
     * 读取一个文档
     *
     * @return
     */
    public DocBuilder read(Object content) {
        return read(content, f -> f);
    }

    public DocBuilder read(Object content, Function<String, String> filter) {

        try {
            Reader reader;
            if (content instanceof String) {
                reader = new StringReader((String) content);
            } else if (content instanceof File) {
                reader = new FileReader((File) content);
            } else if (content instanceof InputStream) {
                reader = new InputStreamReader((InputStream) content);
            } else if (content instanceof Reader) {
                reader = (Reader) content;
            } else {
                throw new IllegalArgumentException("不支持的输入:" + content);
            }
            read(reader, filter);
            reader.close();
            return this;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public DocBuilder read(Reader reader) {
        return read(reader, t -> t);
    }

    public DocBuilder read(Reader reader, Function<String, String> filter) {
        BufferedReader bufferedReader = new BufferedReader(reader);
        try {
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine == null) {
                    break;
                }
                String r = filter.apply(readLine);
                if (r != null) {
                    add(r);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return this;
    }


    public Doc build() {

        Doc doc = new Doc();

        doc.lines = new ArrayList<>();
        if (lines.size() > 0) {

            //第一行
            Line pre = new Line(lines.get(0), 0);
            pre.doc = doc;
            doc.lines.add(pre);
            for (int i = 1; i < lines.size(); i++) {
                Line line = new Line(lines.get(i), i);
                //构建链表关系
                line.doc = doc;
                pre.next = line;
                line.pre = pre;
                pre = line;
                doc.lines.add(line);
            }
        }

        return doc;
    }


}
